package scrolls;

import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.event.KeyEvent;
import java.awt.image.BufferedImage;

public class Player
{
	static int x, y, dx, dy;
	int rotation = 0;
	int imageCounter = 0;
	public static boolean moving = false;
	static int playerEnergy = 150000;
	static int playerLvl = 1;
	static int playerExp = 3;
	static int expNeeded = (((playerLvl + 1) * playerLvl) * 2);
	static int playerHealth = 100;
	static int playerMana = 100;
	static int mapColumn = 6;
	static int mapRow = 8;
	static int playerRow, playerColumn;
	BufferedImage[] sprites = new BufferedImage[8];

	public Player()
	{
		y = 40;
		x = 700;
		getPlayer();
	}

	private void getPlayer()
	{
		BufferedImage player = Resources.player();
		Resources.createArray(player, sprites, 66, 1, 8);
	}

	private void changeImage()
	{
		imageCounter += 2;
		if (imageCounter >= 80)
			imageCounter = 0;
		System.out.println(mapRow + " " + mapColumn);
	}

	public void move()
	{
		y = y + dy;
		x = x + dx;
		changeImage();
		playerPosition();
	}

	static void mapPosition()
	{
		if (y < 0)
			playerRow = 0;
		else
			playerRow = (y / 32) + 1;
		if (x < 0)
			playerColumn = 0;
		else
			playerColumn = (x / 32) + 1;
	}

	private void playerPosition()
	{
		if (mapColumn <= 0)
		{
			if (x >= 817 - 59)
			{
				x = -24;
				mapRow++;
			}
			if (x <= -25)
			{
				x = -25;
			}
			if (y <= -25)
			{
				y = 599 - 152 - 41;
				mapColumn--;
			}
			if (y >= 599 - 152 - 40)
			{
				y = -24;
				mapColumn++;
			}
		} else if (mapRow <= 0)
		{
			if (x >= 817 - 59)
			{
				x = 817 - 59;
			}

			if (x <= -25)
			{
				x = 817 - 59;
				mapRow--;
			}

			if (y <= -25)
			{
				y = 599 - 152 - 41;
				mapColumn--;
			}
			if (y >= 599 - 152 - 40)
			{
				y = -24;
				mapColumn++;
			}
		} else if (mapColumn >= 7)
		{
			if (x >= 817 - 59)
			{
				x = 817 - 59;
				mapRow++;
			}

			if (x <= -25)
			{
				x = 817 - 59;
				mapRow--;
			}

			if (y <= -25)
			{
				y = 599 - 152 - 41;
				mapColumn--;
			}
			if (y >= 599 - 152 - 40)
			{
				y = -24;
			}
		} else if (mapRow >= 10)
		{

			if (x >= 817 - 59)
			{
				x = -24;
				mapColumn++;
			}

			if (x <= -25)
			{
				x = 817 - 59;
				mapColumn--;
			}

			if (y <= -25)
			{
				y = 599 - 152 - 41;
				mapRow--;
			}
			if (y >= 599 - 152 - 40)
			{
				y = -24;
			}
		} else if (mapColumn > 0 && mapColumn < 25 && mapRow > 0 && mapRow < 14)
		{
			if (x >= 817 - 59)
			{
				x = -24;
				mapRow++;
			}

			if (x <= -25)
			{
				x = 817 - 59;
				mapRow--;
			}

			if (y <= -25)
			{
				y = 599 - 152 - 41;
				mapColumn--;
			}
			if (y >= 599 - 152 - 40)
			{
				y = -24;
				mapColumn++;
			}
		}
	}

	public static int playerExp()
	{

		return playerExp;
	}

	public static int getNextExp()
	{
		return expNeeded;
	}

	public static int playerLvl()
	{
		if (playerExp >= expNeeded)
		{
			playerLvl++;
		}
		return playerLvl;
	}

	public static int playerHealth()
	{
		return playerHealth;
	}

	public static int playerMana()
	{
		return playerMana;
	}

	public static int playerEnergy()
	{
		if ((dx != 0) || (dy != 0))
			playerEnergy -= 2;
		if ((dx != 0) && (dy != 0))
			playerEnergy -= 3;

		return playerEnergy;
	}

	public int getX()
	{
		return x;
	}

	public int getY()
	{
		return y;
	}

	public static BufferedImage rotate(BufferedImage image, double angle)
	{
		double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
		int w = image.getWidth(), h = image.getHeight();
		int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);

		GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
		BufferedImage result = gc.createCompatibleImage(neww, newh, Transparency.TRANSLUCENT);
		Graphics2D g = result.createGraphics();

		g.translate((neww - w) / 2, (newh - h) / 2);
		g.rotate(angle, w / 2, h / 2);
		g.drawRenderedImage(image, null);
		g.dispose();

		return result;
	}

	public BufferedImage getPlayerImage()
	{

		roatePlayer();
		int image = animatePlayer();

		double angle = Math.toRadians(rotation);

		if (dy != 0 || dx != 0)
		{
			return rotate(sprites[image], angle);
		}
		return rotate(sprites[0], angle);

	}

	private int animatePlayer()
	{
		return imageCounter / 10;
	}

	private void roatePlayer()
	{
		if (dy > 0)
			rotation = 0;
		if (dy < 0)
			rotation = 180;
		if (dx > 0)
			rotation = -90;
		if (dx < 0)
			rotation = 90;

		if (dy > 0 && dx > 0)
			rotation = -45;
		if (dy > 0 && dx < 0)
			rotation = 45;
		if (dy < 0 && dx < 0)
			rotation = 135;
		if (dy < 0 && dx > 0)
			rotation = -135;
	}

	public void keyPressed(KeyEvent e)
	{
		int key = e.getKeyCode();

		if (key == KeyEvent.VK_A)
		{
			dx = -2;
			rotation = -90;
		}

		if (key == KeyEvent.VK_S)
		{
			dy = 2;
			rotation = 0;
		}

		if (key == KeyEvent.VK_D)
		{
			dx = 2;
			rotation = 90;
		}

		if (key == KeyEvent.VK_W)
		{
			dy = -2;
			rotation = 180;
		}

	}

	public void keyReleased(KeyEvent e)
	{
		int key = e.getKeyCode();

		if (key == KeyEvent.VK_A)
			dx = 0;

		if (key == KeyEvent.VK_S)
			dy = 0;

		if (key == KeyEvent.VK_D)
			dx = 0;

		if (key == KeyEvent.VK_W)
			dy = 0;
	}

}
